/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2021-2023. All rights reserved.
 *
 */

import path from 'path';

import { PathUtil } from '@ohos/hvigor';
import fse from 'fs-extra';

import { ModuleType } from '../../../enum/module-type-enum.js';
import { TaskNames } from '../../common/task-names.js';
import { PrepareQuickfix } from '../../prepare-quickfix.js';
import { ModuleTaskService } from '../../service/module-task-service.js';
import { TargetTaskService } from '../../service/target-task-service.js';
import { AbstractModuleHookTask } from '../abstract-module-hook-task.js';

export class AssembleHqf extends AbstractModuleHookTask {
  static isAdd = true;
  static versionCode: number | undefined = undefined;
  constructor(moduleService: ModuleTaskService) {
    super(moduleService, false, TaskNames.CommonHookTask.ASSEMBLE_HQF);
  }

  initTaskDepends(taskTargetService: TargetTaskService): void {
    throw new Error('Method not implemented.');
  }

  registryAction = () => {
    return async () => {
      const logFile: string = path.resolve(PathUtil.getHvigorCacheDir(), 'outputs', 'patch', 'patch.json');
      let hqfVersion = await PrepareQuickfix.getCurrentHqfVersion();
      if (AssembleHqf.isAdd) {
        hqfVersion += 1;
        AssembleHqf.isAdd = false;
        await fse.ensureDir(path.dirname(logFile));
        await fse.writeJSON(logFile, { patchVersionCode: hqfVersion });
      }
    };
  };
}
